package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmpleeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;

@Controller
@RequestMapping("employee")
public class EmpleeController {
    @Autowired
    EmpleeService empleeService;

    @PostMapping("login")
    @ResponseBody
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        R<Employee> employeeR = empleeService.login(employee, request);
        return employeeR;
    }


    @PostMapping("logout")
    @ResponseBody
    public R<String> logout(HttpServletRequest request) {

        request.getSession().invalidate();

        return R.success("退出成功");
    }

    @PostMapping
    @ResponseBody
    public R<String> save(/*HttpServletRequest request, */@RequestBody Employee employee) {

        //员工密码默认是123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
       /* employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        Long Id = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(Id);
        employee.setUpdateUser(Id);*/

        empleeService.save(employee);

        return R.success("新增员工成功");
    }


    @GetMapping("page")
    @ResponseBody
    public R<Page<Employee>> page( Integer page, Integer pageSize, String name) {
        //page=1&pageSize=30
        //创建分页对象以及实现动态sql对象

        Page<Employee> pages = empleeService.page(page, pageSize, name);

        return R.success(pages);

    }

    @PutMapping
    @ResponseBody
    public R<String> update(/*HttpServletRequest request,*/@RequestBody Employee employee) {


        empleeService.updateById(employee);

        return R.success("修改状态成功");
    }



    @GetMapping("{id}")
    @ResponseBody
    public R<Employee> selectById(HttpServletRequest request,@PathVariable Long id) {


        Employee byId = empleeService.getById(id);

        return R.success(byId);
    }
}
